Skip to content

fix(database): use SchemaRegistry in DiffCommand and MigrateCommand for extender merge#67

Merged
markshust merged 2 commits into
marko-php:developfrom
michalbiarda:fix/db-commands-extender-merge
May 24, 2026
Merged

fix(database): use SchemaRegistry in DiffCommand and MigrateCommand for extender merge#67
markshust merged 2 commits into
marko-php:developfrom
michalbiarda:fix/db-commands-extender-merge

Conversation

@michalbiarda
Copy link
Copy Markdown
Contributor

Summary

  • DiffCommand and MigrateCommand used a single-pass loop (metadataFactory->parse + schemaBuilder->build per entity) to build the entity schema, bypassing the two-pass extender merge in SchemaRegistry::registerEntities()
  • Replaced the loop with SchemaRegistry::registerEntities() in both commands
  • Removed the now-unused EntityMetadataFactory and SchemaBuilder constructor dependencies; updated test helpers accordingly

Closes #66

Steps to reproduce (before fix)

  1. Define a parent entity with #[Table(name: 'products')]
  2. Define an extender entity with #[Table(extends: Product::class)] that adds a column
  3. Run db:migrate — extender column is never added to the table
  4. If the column already exists in the DB, run db:diff — reports [DESTRUCTIVE] Drop column

Expected behavior

  • db:migrate generates and applies an ADD COLUMN migration for the extender column
  • db:diff reports no changes once the column exists in the database

Test plan

  • Run pest tests/Command/DiffCommandTest.php — all green
  • Run pest tests/Command/MigrateCommandTest.php — all green

🤖 Generated with Claude Code

…or extender merge

Single-pass buildEntitySchema loop called schemaBuilder->build() per entity
independently, so extender columns (Table(extends:)) were never merged into
the parent table schema. Commands saw extender columns as missing from the
entity definition — reporting existing ones as destructive drops and never
generating ADD COLUMN for new ones.

Replaced the loop with SchemaRegistry::registerEntities(), which already
implements the correct two-pass merge. Removed the now-unused
EntityMetadataFactory and SchemaBuilder constructor dependencies from both
commands; updated test helpers accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
… commands

DiffCommandTest builds an extender entity (BasicExtenderEntity extends
ExtenderParentEntity), seeds the introspector with the schema produced by
SchemaRegistry, and asserts "No changes detected" — pre-fix this would
report "Drop column: extra" as destructive.

MigrateCommandTest wires a capturing DiffCalculator and asserts the
entitySchema passed to it contains both `id` (parent) and `extra`
(extender), proving the merge happened before the diff was computed.

Co-Authored-By: Michał Biarda <1135380+michalbiarda@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@markshust
Copy link
Copy Markdown
Collaborator

Thanks @michalbiarda — fix is exactly right. SchemaRegistry::registerEntities() already implements the correct two-pass merge, so delegating to it is the cleanest possible solution and avoids drift between the registry and the commands.

Maintainer changes

Added two regression tests so this can't silently regress in the future:

  • DiffCommandTest — wires the real SchemaRegistry into both the entity discovery and the introspector (so the DB matches the merged shape) and asserts "No changes detected". Pre-fix this would have reported Drop column: extra as destructive.
  • MigrateCommandTest — uses a capturing DiffCalculator to inspect the entitySchema passed in, and asserts both the parent's id and the extender's extra column are present, proving the merge ran before the diff.

Both tests reuse the existing ExtenderParentEntity / BasicExtenderEntity fixtures. Also extended Helpers::createDiffCommand() with an entities parameter to support wiring real entity classes into the discovery stub.

Full suite green (5138 passing). Merging via merge commit to preserve your authorship.

@markshust markshust merged commit 3b85d13 into marko-php:develop May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DiffCommand and MigrateCommand skip extender column merge

2 participants